package com.example.weather.old.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weather.R;
import com.example.weather.old.bean.DailyForecast;
import com.example.weather.utils.IconUtils;

import java.util.List;

public class WeatherAdapter extends RecyclerView.Adapter<WeatherAdapter.ViewHolder> {
    private final List<DailyForecast> forecasts;

    public WeatherAdapter(List<DailyForecast> forecasts) {
        this.forecasts = forecasts;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_forecast, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DailyForecast daily = forecasts.get(position);
        holder.tvDate.setText(daily.getDate());
        holder.tvTmp.setText("最高温度：" + daily.getTmp_max() + "\n最低温度：" + daily.getTmp_min());
        holder.tvPop.setText("降水概率：" + daily.getPop());
        holder.ivPic.setImageResource(IconUtils.getDayIconDark(daily.getCond_code_d()));
    }

    @Override
    public int getItemCount() {
        return forecasts.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivPic;
        TextView tvDate;
        TextView tvTmp;
        TextView tvPop;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivPic = itemView.findViewById(R.id.iv_image);
            tvDate = itemView.findViewById(R.id.tv_date);
            tvTmp = itemView.findViewById(R.id.tv_temperature);
            tvPop = itemView.findViewById(R.id.tv_pop);
        }
    }
}
